package com.net.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.net.dao.SuperDao;
import com.net.dao.ProviderDao;
import com.net.model.Commodity;
import com.net.model.Provider;
@Component("providerDao")
public class ProviderDaoImpl extends SuperDao implements ProviderDao{

	public boolean delete(Provider u) {
		Provider cust = (Provider) this.getHibernateTemplate().load(Provider.class, u.getId());
		if(cust!=null){
			this.getHibernateTemplate().delete(cust);
			return true;
		}else
			return false;
	}

	@SuppressWarnings("unchecked")
	public List<Provider> getProviders() {
		return (List<Provider>)this.getHibernateTemplate().find("from Provider");
	}

	public Provider loadProviderById(int id) {
		return (Provider) this.getHibernateTemplate().load(Provider.class, id);
	}

	@SuppressWarnings("unchecked")
	public Provider loadProviderByName(String name) {
		List<Provider> providers = getHibernateTemplate().find("from Provider c where c.Provider_name='"+name+"'");
		if(providers!=null&&providers.size()>0){
			return providers.get(0);
		}else
			return null;
	}

	public void save(Provider u) {
		this.getHibernateTemplate().save(u);
	}

	public void update(Provider provider) {
		this.getHibernateTemplate().update(provider);
	}

	public List<Provider> searchList(String param1, String param2, String param3) {
		String hql = "";
		if(!param1.equals("")&&!param2.equals("")){
			if(!param3.equals("")){
			String[] str = param3.split("\\.");
			if(str.length==2){
				hql = "from Provider c where c."+param1+" like '%"+param2+"%' order by c."+ str[0] + " " + str[1];
				System.out.println(hql);
			}else{
				hql = "from Provider c where c."+param1+" like '%"+param2+"%' order by c."+ str[0];
				System.out.println(hql);
			}
			}else{
				hql = "from Provider c where c."+param1+" like '%"+param2+"%'";
			}
			return (List<Provider>)this.getHibernateTemplate().find(hql);
		}
		return null;
	}

	public int getCount() {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from Provider").list();
			}
		};
		return getHibernateTemplate().executeFind(hc).size();
	}

	public List<Provider> getProviderByCondition(final int currentPage, final int perPage,
			final String condition) {
		HibernateCallback hc = new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String str = "from Provider p where " + condition;
				System.out.println(str);
				return session.createQuery(str).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
			}
			
		};
		return getHibernateTemplate().executeFind(hc);
	}

	public List<Provider> getProviders(final int currentPage, final int perPage,
			final String orderType, final String orderName) {
		System.out.println("99999999999");
		HibernateCallback hc = new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				if(orderType==null||orderName==null){
				return session.createQuery("from Provider").setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}else{
					System.out.println("from Provider order by "+orderName+" "+ orderType);
					return session.createQuery("from Provider order by "+orderName+" "+ orderType).setFirstResult(currentPage*perPage).setMaxResults(perPage).list();
				}
			}
		};
		
		System.out.println(getHibernateTemplate().executeFind(hc));
		return (List<Provider>)getHibernateTemplate().execute(hc);
	}

}
